var GameData = require('../module/gameData')();
var goal = 700;
cc.Class({
    extends: cc.Component,

    properties: {
        prefab_fish: cc.Prefab, /* 各种鱼的纹理集 */
        movefishAtlas: cc.SpriteAtlas, /* 所有鱼的图集 */
        mfishSp: cc.Sprite,
        moveFishPool: null,
        sp1: null,
        sp2: null,
        AllMoveFishArr: null,

    },
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        this.moveFishPool = new cc.NodePool();
        this.AllMoveFishArr = [];
        this.Addmovefish();
    },
    onEnable() {
    },
    getLeftObj(is_direction) {
        /*选择左边的移动鱼的数量*/
        let moveNum = Math.floor(Math.random() * 4);
        for (var a = 0; a < moveNum; a++) {
            /*选择这种鱼的类型*/
            let f_type = Math.floor(Math.random() * 7 + 1);
            let _x, _y;
            if (is_direction == 1) {
                /*设置鱼在左边的位置*/
                _x = -Math.floor(Math.random() * 300 + goal);
                _y = Math.floor(Math.random() * 320);
            } else {
                _x = Math.floor(Math.random() * 300 + goal);
                _y = Math.floor(Math.random() * 320);
            }
            this.CreateMovefish(is_direction, f_type, _x, _y);
        }
    },
    CreateMovefish(num, f_type, _x, _y) {
        let _fish;
        if (this.moveFishPool.size() > 0) {
            _fish = this.moveFishPool.get();
            _fish.spriteFrame = this.movefishAtlas.getSpriteFrame("movefish" + f_type);
        } else {
            _fish = cc.instantiate(this.prefab_fish);
            _fish.getComponent(cc.Sprite).spriteFrame = this.movefishAtlas.getSpriteFrame("movefish" + f_type);
        }
        _fish.active=true;
        if (num == 1) {
            _fish.direction = true;
            /*设置鱼的移动方向朝右 反之朝左*/
            _fish.scaleX = -1;
            /*旋转鱼头的朝向 与鱼移动方向一致*/
        } else {
            _fish.direction = false;
            _fish.scaleX = 1;
        }
        _fish.parent = this.node;
        /*
        1       255
        200/255 200
        * */
        // let _sx=Math.floor(Math.random()*55+200);
        // _fish.scaleX=_sx/255;
        // _fish.scaleY=_sx/255;
        // _fish.opacity=_sx;
        // console.log(_sx)
        let _speed = GameData.mvoeSpeedData[f_type - 1];
        // _fish.endPoint=cc.v2()
        _fish.speed = _speed;
        _fish.x = _x;
        _fish.y = _y;
        this.AllMoveFishArr.push(_fish);
    },

    start() {

    },
    Addmovefish() {
        if (this.AllMoveFishArr.length <= 0) {
            this.isMove = false;
            this.getLeftObj(1);
            this.getLeftObj(0);
            this.isMove = true;
            this.Addmovefish();
        }
    },
    update(dt) {
        if (this.isMove) {
            for (var a = 0; a < this.AllMoveFishArr.length ; a++) {
                if (this.AllMoveFishArr[a].direction ) {
                    this.AllMoveFishArr[a].x += this.AllMoveFishArr[a].speed;
                    if (this.AllMoveFishArr[a].x > goal) {
                        this.moveFishPool.put(this.AllMoveFishArr[a]);
                        this.AllMoveFishArr.splice(a, 1);
                        a--;
                        this.Addmovefish();
                    }
                } else {
                    this.AllMoveFishArr[a].x += -this.AllMoveFishArr[a].speed;
                    if (this.AllMoveFishArr[a].x < -goal) {
                        this.moveFishPool.put(this.AllMoveFishArr[a]);
                        this.AllMoveFishArr.splice(a, 1);
                        a--;
                        this.Addmovefish();
                    }
                }
            }
        }

    },
});
